<?php

namespace app\model;

use think\facade\Cache;
use think\Model;

class BocProduct extends Model
{
    protected $table = 'boc_product';

    /**
     * 上一篇 下一篇.
     */
    public function Page_news($id, $ctype = '')
    {
        $list = self::field('id,title')->where(['cate_id' => $ctype, 'status' => 1, 'is_deleted' => 0])->order('sort desc,create_at desc')->select()->toarray();

        $kkid = '';
        foreach ($list as $k => $v) {
            if ($v['id'] == $id) {
                $kkid = $k;
                break;
            }
        }
        $data['pre'] = '';
        $data['next'] = '';
        if (!empty($list[$kkid - 1])) {
            $data['pre'] = $list[$kkid - 1];
        }
        if (!empty($list[$kkid + 1])) {
            $data['next'] = $list[$kkid + 1];
        }

        return $data;
    }

    /**
     * 查询首页置顶数据.
     */
    public function index_audit($field = 'id,title,intro,cover,left(create_at,4) as y,substring(create_at,6,5) as d,thumb,url', $limit = 6, $audit = 1)
    {
        $cache_key = 'product-index-audit';
        $list = Cache::get($cache_key);
        if (empty($list)) {
            $arr = $this->field($field)->where(['audit' => $audit])
            ->where(['is_deleted' => 0, 'status' => 1])
            ->order('sort desc,create_at desc')
            ->limit($limit)
            ->select();
            $list = $arr ? $arr->toarray() : [];
            Cache::tag('product')->set($cache_key, $list);
        }

        return $list;
    }

    /**
     * 查询置顶数据.
     */
    public function get_audit($field = 'id,cate_id,title,en_title,cover', $audit = 1)
    {
        $cache_key = 'product-audit';
        $list = Cache::get($cache_key);
        if (empty($list)) {
            $arr = $this->field($field)->where(['audit' => $audit])
            ->where(['is_deleted' => 0, 'status' => 1])
            ->order('sort desc,create_at desc')
            ->select();
            $list = $arr ? $arr->toarray() : [];
            Cache::tag('product')->set($cache_key, $list);
        }

        return $list;
    }

    /**
     * 获取详情页随机数据.
     */
    public function Rand_list($id, $type)
    {
        $where = [
            ['id', '<>', $id],
            ['cate_id', '=', $type],
            ['status', '=', '1'],
            ['is_deleted', '=', 0],
        ];

        return $this->field('id,cate_id,title,cover,thumb')->where($where)->limit(4)->orderRaw('RAND()')->select()->toArray();
    }

    /**
     * 查看详情.
     */
    public function get_one($id, $field = 'id,title,title_seo,keywords,description,content,imgcontent,bg_photo')
    {
        $where = [
            ['id', '=', $id],
            ['status', '=', 1],
            ['is_deleted', '=', 0],
        ];

        $list = $this->field($field)->where($where)->find();

        return $list ? $list->toarray() : [];
    }

    /*
     * 获取列表数据
     */
    public function get_list($filed = '*')
    {
        $cache_key = 'product-list';
        $list = Cache::get($cache_key);
        if (empty($list)) {
            $list = $this->field($filed)
                ->where(['status' => 1, 'is_deleted' => 0])
                ->order('sort desc,create_at desc')
                // ->limit(6)
                ->select()->toArray();
            Cache::tag('product')->set($cache_key, $list);
        }

        return $list;
    }

    /**
     * 分页.
     */
    public function page_list($where = ['is_deleted' => 0, 'status' => 1], $field = 'id,title,cover,intro', $list_rows = 9)
    {
        $list = $this->field($field)->where($where)->order('sort desc,create_at desc')->paginate(['list_rows' => $list_rows, 'fragment' => 'pro', 'query' => request()->param()]);

        return $list;
    }

    /**
     * 搜索.
     */
    public function get_search_list($where = ['status' => 1, 'audit' => 0, 'is_deleted' => 0])
    {
        $list = $this->field('id,title,intro,left(create_at,10) as create_at')
                ->where($where)
                ->order('sort desc,create_at desc')
                ->select()->toArray();

        return $list;
    }
}
